package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.LkBodyDao;
import com.itheima.dao.LkIllnessDao;
import com.itheima.dao.LkPsychologyDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.pojo.IllnessMember;
import com.itheima.pojo.Member;
import com.itheima.pojo.RiskAssessment;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private LkIllnessDao illnessDao;
    @Autowired
    private LkBodyDao bodyDao;
    @Autowired
    private LkPsychologyDao psychologyDao;

    //根据手机号查询会员信息
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    @Override
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if (months != null && months.size() > 0) {
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    //根据病情查询患者信息
    @Override
    public PageResult findIllnessMember(QueryPageBean pageBean) {
        String queryString = pageBean.getQueryString();
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<IllnessMember> page = memberDao.findIllnessMember(queryString);
        System.out.println(page.getResult());
        return new PageResult(page.getTotal(), page.getResult());

    }

    //根据id删除病人该项信息
    @Override
    public void delectByIllnessIdAndMemberId(String id, String memberId) {
        Map<String, String> map = new HashMap<>();
        map.put("Illnessid", id);
        map.put("MemberId", memberId);
         memberDao.delectByIllnessIdAndMemberId(map);
    }

    //lk 根据客户患病情况查询客户的风险评估情况
    @Override
    public PageResult findPage4MenberRisk(QueryPageBean pageBean) {
        //1.获取分页查询需要的条件
        Integer pageSize = pageBean.getPageSize();
        Integer currentPage = pageBean.getCurrentPage();
        String queryString = pageBean.getQueryString();
        Integer begin = (currentPage - 1) * pageSize;
        //2.封装查询需要的数据，调用dao查询，获得结果List
        Map<String, Object> limitMap = new HashMap<>();
        limitMap.put("begin", begin);
        limitMap.put("pageSize", pageSize);
        limitMap.put("queryString", queryString);


        ////

        List<RiskAssessment> riskAssessmentList = memberDao.findMember4Risk(limitMap);
        //遍历获取到的对象,封装其他数据
        for (RiskAssessment riskAssessment : riskAssessmentList) {
            //封装严重级别	severityLevel
            Integer severityLevelCount = riskAssessment.getSeverityLevelCount();
            if (severityLevelCount == 0) {
                riskAssessment.setSeverityLevel("健康");
            } else if (severityLevelCount == 1) {
                riskAssessment.setSeverityLevel("正常");
            } else if (severityLevelCount == 2) {
                riskAssessment.setSeverityLevel("亚健康");
            } else if (severityLevelCount > 2) {
                riskAssessment.setSeverityLevel("严重");
            }
            //封装评估日期 evaluateDate 获取body表中的assessment_data 评估时间
            Date assessmentDate = bodyDao.findAssessmentDateByMemberId(riskAssessment.getId());
            String evaluateDate = new SimpleDateFormat("yyyy年MM月dd日").format(new Date()).toString();
            riskAssessment.setEvaluateDate(evaluateDate);
            //封装报告状态 reportStatus
            riskAssessment.setReportStatus("未出报告");
            //封装操作人 operator
            riskAssessment.setOperator("王医生");
        }
        //获取总页码
        int totalCount = memberDao.findMemberTotalCount();
        Long total = new Long((long) totalCount);

//        封装到PageResult
        PageResult pageResult = new PageResult(total, riskAssessmentList);

        return pageResult;

    }

    @Override
    public Map<String, Object> findMemberEcho(Integer id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
//  formData: {}  Member对象 ,//编辑窗口中基本信息表单数据 1
        Member member = memberDao.findById(id);
        resultMap.put("formData",member);
//  illnussTableData:[]  List<Illness> ,//疾病信息表中展示疾病表单数据 2
        List<Illness> allIllness = illnessDao.findAllIllness();
        resultMap.put("illnussTableData",allIllness);
//        illnussIds:[] Integer[]  ,//疾病信息表中选中的id集合 2
        Integer[] illnussIds = illnessDao.findMemberAndIllnessById(id);
        resultMap.put("illnussIds",illnussIds);
//        bodyCheckList:[]  Integer[],//身体状况中选中的集合 [1/2/3] 3
        Body body = bodyDao.findBodyByMemberId(id);
        List<String> bodyList = new ArrayList<String>();
        if(body != null) {
            if (body.getDeficiency() == 1) {
                bodyList.add("1");
            }
            if (body.getYandeficiency() == 1) {
                bodyList.add("2");
            }
            if (body.getYindeficiency() == 1) {
                bodyList.add("3");
            }
        }
        resultMap.put(" bodyCheckList",bodyList);
//        psychologyCheckList:[]  Integer[] ,//心理状况表中选中的集合 [1/2/3] 4
        Psychology psychology = psychologyDao.findPsychologyByMemberId(id);
        List<String> psychologyList = new ArrayList<String>();
        if(psychology != null) {
            if (psychology.getObsession() == 1) {
                psychologyList.add("1");
            }
            if (psychology.getAnxious() == 1) {
                psychologyList.add("2");
            }
            if (psychology.getParanoia() == 1) {
                psychologyList.add("3");
            }
        }
        resultMap.put(" psychologyCheckList",psychologyList);
        return resultMap;
    }
}
